package NewCharacter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Demo5_StreamAPI {

    /**
     * 创建Stream流
     * @return
     */
    public Stream<String> createStream(){

        List<String> animals = new ArrayList<>();

        animals.add("panda");
        animals.add("monkey");
        animals.add("tiger");
        animals.add("lion");
        animals.add("pip");

        //通过已有集合 创建 Stream
        Stream<String> animalStream = animals.stream();

        //通过不定长参数，创建 Stream
        Stream<String> fruitStream = Stream.of("apple", "banana", "watermelon", "peach", "pear",
                "pineapple","orange","strawberry");

        return fruitStream;
    }

    public void useStream(Stream<String> stream){

        stream
            //对Stream中的每个元素执行操作
            .map((str) -> str+"^-^")
            .sorted((o1,o2) -> {
                String thisSecondChar = o1.substring(1,2);
                String thatSecondChar = o2.substring(1,2);
                return thatSecondChar.compareTo(thisSecondChar);
            })
            //多集合中的元素进行过滤，返回值为true的保留，为false的过滤掉了
            .filter(str -> !str.startsWith("a"))
            //取前n个元素
            .limit(6)
            //跳过前n个元素，从第n+1个元素开始
            .skip(2)
            //终结方法 遍历输出
//            .forEach(System.out::println);

            //终结方法 返回当前Stream有几个元素
//            .count();

            //终结方法 可以把Stream中的元素以集合的类型返回
            .collect(Collectors.toList());

    }

    public static void main(String[] args) {

        Demo5_StreamAPI streamAPI = new Demo5_StreamAPI();

        Stream<String> oriStream = streamAPI.createStream();

        streamAPI.useStream(oriStream);

    }

}
